package com.cmccsi.service.impl;



import java.util.List;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import com.cmccsi.entity.qxuserDto;
import com.cmccsi.mapper.qxuserDtoMapper;
import com.cmccsi.service.QxUserService;
import com.cmccsi.utils.MybatisUtil;
import com.cmccsi.utils.RedisConnPool;
import com.cmccsi.utils.RedisKeyProperties;

public class QxUserRedisServiceImpl implements QxUserService {
	
	private static final Logger logger = Logger.getLogger(QxUserRedisServiceImpl.class);
	@Override
	public Boolean save(qxuserDto user,int optType) {
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "user");
			json.put("id", user.getId());
			if(optType==0)
			{
				json.put("optype", "insert");
			}
			else
			{
				json.put("optype", "update");
			}
			sc.lpush(RedisKeyProperties.sync_db, json.toString());			
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public Boolean update(qxuserDto user) {
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "user");
			json.put("id", user.getId());			
			json.put("optype", "update");			
			sc.lpush(RedisKeyProperties.sync_db, json.toString());			
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public qxuserDto find(Long id) {

		return null;
	}

	@Override
	public Boolean delete(Long id) {
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "user");
			json.put("id", id);			
			json.put("optype", "delete");			
			sc.lpush(RedisKeyProperties.sync_db, json.toString());			
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}
	
	
	@Override
	public Boolean deleteByMobile(String Mobile) {
		
		return false;
	}
	
	@Override
	public Boolean deleteByDomainId(String DomainId) {

		return false;
	}
}
